package com.abhimanyu.dataaccess.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.abhimanyu.dataaccess.IDao;
import com.abhimanyu.dataaccess.model.DollarRate;

public class DollarRateDao implements IDao<DollarRate> {

	JdbcTemplate jdbcTemplate;

	public List<DollarRate> fetchAll() {

		List<DollarRate> dollarRateList = jdbcTemplate.query(
				"SELECT * FROM DOLLARRATE", new RowMapper<DollarRate>() {
					public DollarRate mapRow(ResultSet rs, int index)
							throws SQLException {
						DollarRate dollarRate = new DollarRate();
						dollarRate.setId(rs.getLong("id"));
						dollarRate.setDollarRate(rs.getString("dollarrate"));
						dollarRate.setDate(rs.getDate("date"));
						dollarRate.setServiceName(rs.getString("servicename"));
						return dollarRate;
					}
				});
		return dollarRateList;
	}

	public void save(DollarRate dollarRate) {

		jdbcTemplate
				.update("INSERT INTO DOLLARRATE (id,dollarrate, date,servicename) values ((SELECT MAXID FROM (SELECT MAX(ID) + 1 AS MAXID FROM DOLLARRATE) AS D),?,?,?)",
						dollarRate.getDollarRate(), dollarRate.getDate(),
						dollarRate.getServiceName());
	}

	public void update(DollarRate object) {
		// TODO Auto-generated method stub

	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
}
